﻿using EventBus;
using LiteStep.Currencies;
using LiteStep.Exchanges.Vircurex;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteStep.Arbitrage
{
    public class TriangleCalculatorBuilder
    {
        public static void CreateAndRegisterAllVircurexTriableCalculators(IEventBus eventBus)
        {
            var exchanges = new List<Type> { typeof(Vircurex) };

            var currencies = new List<Type> {
                typeof(Btc),
                //typeof(Bqc),
                //typeof(Cnc),
                //typeof(Dvc),
                typeof(Eur),
                //typeof(Frc),
                //typeof(Ftc),
                //typeof(Ixc),
                typeof(Ltc),
                //typeof(Nmc),
                //typeof(Nvc),
                //typeof(Ppc),
                //typeof(Trc),
                typeof(Usd),
                //typeof(Yac)
            };

            CreateAndRegisterCalculatorsFor(exchanges, currencies, eventBus);

            //Create(typeof(Btc), typeof(Usd), typeof(Ltc), typeof(Vircurex)).Register(eventBus);
        }

        public static void CreateAndRegisterCalculatorsFor(IList<Type> exchanges, IList<Type> currencies, IEventBus eventBus)
        {
            foreach (var exchange in exchanges) {
                for (var i = 0; i < currencies.Count; i++)
                {
                    for (var j = 0; j < currencies.Count; j++)
                    {
                        if (j == i) continue;

                        for (var k = 0; k < currencies.Count; k++)
                        {
                            if (k == i || k == j) continue;

                            var calculator = Create(currencies[i], currencies[j], currencies[k], exchange);
                            calculator.Register(eventBus);
                        }
                    }
                }
            }
        }

        public static dynamic Create(string tOneName, string tTwoName, string tThreeName, string tExchange)
        {
            Type targetOne = Type.GetType(tOneName);
            Type targetTwo = Type.GetType(tTwoName);
            Type targetThree = Type.GetType(tThreeName);
            Type targetExchange = Type.GetType(tExchange);

            return Create(targetOne, targetTwo, targetThree, targetExchange);
        }

        public static dynamic Create(Type targetOne, Type targetTwo, Type targetThree, Type targetExchange)
        {
            Type genericTriangleCalculator = typeof(TriangleCalculator<,,,>).MakeGenericType(targetOne, targetTwo, targetThree, targetExchange);

            return Activator.CreateInstance(genericTriangleCalculator);
        }
    }
}
